package com.weixx.yctp.user.service;

import cc.eamon.open.status.StatusException;
import com.weixx.yctp.user.dao.RoleDao;
import com.weixx.yctp.user.dao.UserDao;
import com.weixx.yctp.user.dao.UserRoleDao;
import com.weixx.yctp.user.entity.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


@Service
public class UserRoleService {
    @Autowired
    private UserRoleDao userRoleDao;
    @Autowired
    private UserDao userDao;
    @Autowired
    private RoleDao roleDao;

    public int setUserRole(Integer userId,Integer roleId,String name) throws StatusException {
        Role entity = roleDao.selectByPrimaryKey(roleId);
        if (entity == null) throw new StatusException("ROLE_NULL");

        User user = userDao.selectByPrimaryKey(userId);
        if (user == null) throw new StatusException("USER_NULL");

        if (name == null) throw new StatusException("未登录");

       return userRoleDao.insertSelective(new UserRole(userId,roleId,name,name));
    }
}
